jQuery.validator.addMethod("endTime", function(value, 	element, param) {
	var startTime = $('#time').val();
	 var endTime = value;
	 
	 var sSplit=startTime.split(":");
	 var eSplit= endTime.split(":");
	 
	 var totalStart = (sSplit[0]*60)+sSplit[1];
	 var totalEnd = (eSplit[0]*60)+eSplit[1];
	 console.log("Time1: "+ totalStart + " Time2: " + totalEnd);

	 return startTime<endTime|| value == "";
	 }, "End Time must be after start Time");


$.validator.addMethod("nextFollowUpTypeValidate", function(value, element, params) {
	if($('input[name=nextFollowUp]:radio:checked').val()=='true'){
		if(value==""){
			return false;
		}
    }
    return true;
}, "Please,Select the next follow up type");

$.validator.addMethod("nextFollowUpDateValidate", function(value, element, params) {
	var followUpType = $('#followUpType').val();
	if(followUpType!=""){
		if(value==""){
			return false;
		}
    }
    return true;
}, "Please,Enter the next follow up Date");

jQuery.validator.addMethod("greaterThan", 
		function(value, element, params) {
	
			 var followUpDate = $("#followUpDate").val();
			 var dateComponents = followUpDate.split("/");
			 var follow = new Date(dateComponents[2], dateComponents[1] - 1, dateComponents[0]);
			 
			 var dateInput = $("#date").val();
			 var dateComponents = dateInput.split("/");
			 var date = new Date(dateComponents[2], dateComponents[1] - 1, dateComponents[0]);
			 
			  if (follow<= date) {
			      return false;
			  }
			  return true;
		},'Please,Enter follow up date bigger than date');
	
function radioOnClick(){
	if($('input[name=nextFollowUp]:radio:checked').val()=='true'){         
   	 $('#followUpDate').removeAttr("disabled");
   	 $('#followUpType').removeAttr("disabled");
   	 $('#followUpDetails').removeAttr("disabled");
   	 $("#followIcon").show();
    }
       else{
         $('#followUpDate').attr("disabled", true); 
         $('#followUpType').attr("disabled", true); 
         $('#followUpDetails').attr("disabled", true); 
         $("#followIcon").hide();
    }
}

function selectOnClick(){
	var selectId = $("#salesMan").val();
	if(selectId!=""){
		$("#salesManId").val(selectId);
	}
}

function selectContactOnClick(){
	var selectId = $("#contactedPerson").val();
	if(selectId!=""){
		$("#contactedPersonId").val(selectId);
	}
}

$(document).ready(function() {
	$("#followIcon").hide();
	 $('#timepicker').datetimepicker({
	      pickDate: false
	    });
	 
	 $('#timepicker2').datetimepicker({
	      pickDate: false
	    });

	var picker = $('#datepicker').datetimepicker({
	      pickTime: false
    });
	
	$('#followupdatepicker').datetimepicker({
	      pickTime: false
	});
	
	$.fn.datetimepicker.defaults = {
			  maskInput: true,           // disables the text input mask
			  pickDate: true,            // disables the date picker
			  pickTime: true,            // disables de time picker
			  pick12HourFormat: false,   // enables the 12-hour format time picker
			  pickSeconds: true,         // disables seconds in the time picker
			  startDate: -Infinity,      // set a minimum date
			  endDate: Infinity          // set a maximum date
			};
	picker.on('changeDate', function(e) {
		var days= ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"];       
        $("#dayName #day").val(days[e.date.getDay()]);
		});
	
	 
	var validator = $("#appointmentForm").validate({
		rules: {
			salesMan: "required",
			contactedPerson: "required",
			date:"required",
			day:"required",
			startTime:"required",
			endTime:{required:true,endTime: "0"},
			followUpType:{nextFollowUpTypeValidate:"0"},
			followUpDate:{nextFollowUpDateValidate:"0",greaterThan: "0"}
		},
		messages: {
			salesMan: {required:"Please,Choose the salesMan"},
			contactedPerson: {required:"Please,Choose the contacted Person"},
			date: {required:"Please,enter the Date of the Appointment"},
			day: {required:"Please,enter the Day"},
			startTime: {required:"Please,enter the start time"},
			endTime: {required:"Please,enter the end time",endTime:"End Time must be after start Time"},
			followUpType:{nextFollowUpTypeValidate:"Please,Select the next follow up type"},
			followUpDate:{nextFollowUpDateValidate:"Please,Select the next follow up Date"}
		},
		errorPlacement: function(error, element) {
			error.appendTo(element.next());
		},
		submitHandler: function() {
			 form.submit();
		}
	});
	cancelButtonHandler();
});

/**
 * Cancel button handler
 */
function cancelButtonHandler(){
	$("#cancelButton").click(function() {
		document.location.href='leads';
	});	
}

